# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=xalan-c
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.11
pkgrel=7
pkgdesc="An XSLT processing library (mingw-w64)"
arch=('any')
url="https://xalan.apache.org/xalan-c"
license=('APACHE')
makedepends=("sed" "${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-xerces-c" "${MINGW_PACKAGE_PREFIX}-tools")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs" "${MINGW_PACKAGE_PREFIX}-xerces-c")
options=('!libtool' 'strip')
source=(https://archive.apache.org/dist/xalan/xalan-c/sources/xalan_c-${pkgver}-src.tar.gz
        mingw-w64-fix.patch)
sha256sums=('4f5e7f75733d72e30a2165f9fdb9371831cf6ff0d1997b1fb64cdd5dc2126a28'
            'cd93106cf2aa569bf83d4d62881b767e44951a7939cd62b6624fab41db31112d')

prepare(){
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/mingw-w64-fix.patch
}

build() {
  export XERCESCROOT=${MINGW_PREFIX}
  if [ "${MINGW_CHOST}" = "i686-w64-mingw32" ]; then
    export BITSTOBUILD=32
  else
    export BITSTOBUILD=64
  fi
  CXXFLAGS+=" -DNDEBUG -mthreads"
  CPPFLAGS+=""
  CFLAGS+=" -DNDEBUG -mthreads"
  LDFLAGS+=" -mthreads"
  LIBS+=" -mthreads"

  export XALANCROOT="${srcdir}"/${_realname}-${pkgver}/c
  export ICUROOT=${MINGW_PREFIX}
  export XALAN_LOCALE_SYSTEM=inmem
  export XALAN_LOCALE=en_US

  cd "${srcdir}"/${_realname}-${pkgver}/c
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST}
  make -j1
}

package() {
  export XERCESCROOT=${MINGW_PREFIX}
  if [ "${MINGW_CHOST}" = "i686-w64-mingw32" ]; then
    export BITSTOBUILD=32
  else
    export BITSTOBUILD=64
  fi
  CXXFLAGS+=" -DNDEBUG -mthreads"
  CPPFLAGS+=""
  CFLAGS+=" -DNDEBUG -mthreads"
  LDFLAGS+=" -mthreads"
  LIBS+=" -mthreads"

  export XALANCROOT="${srcdir}"/${_realname}-${pkgver}/c
  export ICUROOT=${MINGW_PREFIX}
  export XALAN_LOCALE_SYSTEM=inmem
  export XALAN_LOCALE=en_US

  cd "${srcdir}"/${_realname}-${pkgver}/c
  make DESTDIR="$pkgdir" install
  mv "${pkgdir}${MINGW_PREFIX}"/lib/*.dll "${pkgdir}${MINGW_PREFIX}"/bin/
  find "${pkgdir}${MINGW_PREFIX}" -name '*.bat' -o -name '*.def' -o -name '*.exp' -o -name '*.111' | xargs -rtl1 rm
  pushd "${pkgdir}${MINGW_PREFIX}"/bin > /dev/null
  for ff in *.dll; do
    local nn=$(basename ${ff})
    nn=${nn%.dll}
    ${MINGW_PREFIX}/bin/gendef ${ff}
    ${MINGW_PREFIX}/bin/dlltool -D ${ff} -d ${nn}.def -l ../lib/lib${nn}.dll.a
    rm ${nn}.def
  done
  popd > /dev/null
}
